package com.feemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.core.page.PageDomain;
import com.common.utils.sql.SqlUtil;
import com.feemanage.common.SearchUtils;
import com.feemanage.domain.BizProjectFeeCard;
import com.feemanage.domain.vo.BizProjectFeeCardVo;
import com.feemanage.mapper.bizProjectFeeCardMapper;
import com.feemanage.service.IBizProjectFeeCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BizProjectFeeCardServiceImpl implements IBizProjectFeeCardService {

    @Autowired
    private bizProjectFeeCardMapper baseMapper;

    /**
     * 查询列表
     * @param pageDomain
     * @param feeCard
     * @return
     */
    @Override
    public IPage<BizProjectFeeCardVo> selectList(PageDomain pageDomain, BizProjectFeeCard feeCard) {
        Page<BizProjectFeeCard> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        QueryWrapper<BizProjectFeeCard> wrapper = getQuery(feeCard);
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
        boolean isASC = SqlUtil.escapeOrderBySql(pageDomain.getIsAsc()).equalsIgnoreCase("asc");
        if (!StringUtils.isEmpty(orderByColumn))
        {
            wrapper.orderBy(true,isASC,orderByColumn);
        }
        return baseMapper.selectPageList(page,wrapper);
    }

    /***
     * 导出
     * @param pageDomain
     * @param feeCard
     * @return
     */
    @Override
    public List<BizProjectFeeCardVo> selectExportList(PageDomain pageDomain, BizProjectFeeCard feeCard) {
        QueryWrapper<BizProjectFeeCard> wrapper = getQuery(feeCard);
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
        boolean isASC = SqlUtil.escapeOrderBySql(pageDomain.getIsAsc()).equalsIgnoreCase("asc");
        if (!StringUtils.isEmpty(orderByColumn))
        {
            wrapper.orderBy(true,isASC,orderByColumn);
        }
        return baseMapper.selectPageList(wrapper);
    }

    /**
     * 拼接条件
     * @param feeCard
     * @return
     */
    private QueryWrapper<BizProjectFeeCard> getQuery(BizProjectFeeCard feeCard) {
        QueryWrapper<BizProjectFeeCard> wrapper = Wrappers.query();
        Map<String, Object> params = feeCard.getParams();
        wrapper.eq(StringUtils.isNotBlank(feeCard.getProjectId()), "p.id", feeCard.getProjectId())
                .like(StringUtils.isNotBlank(feeCard.getFeeCardNum()), "c.fee_card_num", feeCard.getFeeCardNum())
                .like(StringUtils.isNotBlank(feeCard.getProjectName()),"p.name",feeCard.getProjectName())
                .like(StringUtils.isNotBlank(feeCard.getFeeChargerName()),"c.fee_charger_name",feeCard.getFeeChargerName())
                .like(StringUtils.isNotBlank(feeCard.getChargerName()),"p.charger_name",feeCard.getChargerName())
                .between(ObjectUtils.isNotEmpty(params.get("beginTime")) && ObjectUtils.isNotEmpty(params.get("endTime")),
                        "p.authorize_date", params.get("beginTime"), params.get("endTime"))
                .ge(ObjectUtils.isNotEmpty(params.get("beginTime")) && ObjectUtils.isEmpty(params.get("endTime")),"p.authorize_date", params.get("beginTime"))
                .le(ObjectUtils.isEmpty(params.get("beginTime")) && ObjectUtils.isNotEmpty(params.get("endTime")),"p.authorize_date", params.get("endTime"))
                .between(ObjectUtils.isNotEmpty(params.get("maxfee")) && ObjectUtils.isNotEmpty(params.get("minfee")),
                        "c.transfer_fee", SearchUtils.processAmount(params.get("minfee")), SearchUtils.processAmount(params.get("maxfee")))
                .ge(ObjectUtils.isNotEmpty(params.get("minfee")) && ObjectUtils.isEmpty(params.get("maxfee")),"c.transfer_fee", SearchUtils.processAmount(params.get("minfee")))
                .le(ObjectUtils.isNotEmpty(params.get("maxfee")) && ObjectUtils.isEmpty(params.get("minfee")),"c.transfer_fee", SearchUtils.processAmount(params.get("maxfee")))
                .like(StringUtils.isNotBlank(feeCard.getProjectCode()),"p.code",feeCard.getProjectCode());

        if(StringUtils.isNotEmpty(feeCard.getProjectType())){
            List<String> list = Arrays.stream(feeCard.getProjectType().split(",")).collect(Collectors.toList());
            wrapper.in("p.project_type",list);
        }

        if(StringUtils.isNotEmpty(feeCard.getProjectClass())){
            List<String> list = Arrays.stream(feeCard.getProjectClass().split(",")).collect(Collectors.toList());
            wrapper.in("p.project_class",list);
        }
        wrapper.groupBy("c.ID ,c.fee_card_num ,p.name ,c.fee_charger_name ,p.charger_name , p.authorize_date,p.project_type,p.code,p.project_class,c.transfer_state ,B.incomeTotal, A.payTotal ,c.transfer_fee ,c.transfer_fee_code");
        return wrapper;
    }


}
